﻿/*
 ***************************************************************************
 * Microsoft Visual C# .NET
 * 名称：       App_Search_HealthArchivesSrhForm
 * 类型：       控制层
 * 功能说明：   健康管家-健康档案查询页面
 * 作者：       章 昕
 * 创建日期：   2007/09/21
 * 修改日期     修改者      修改内容
 * 
 * 
 * ---
 ***************************************************************************
*/
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using log4net;
using Shpm.System;
using Shpm.App.Commons.Db.Beans.Master;
using Shpm.App.Search.Bean;
using Shpm.App.Search.Dao;
using Shpm.App.Commons.Utils;

public partial class App_Search_HealthArchivesSrhForm : System.Web.UI.Page
{
    // log
    protected static ILog log = LogManager.GetLogger(typeof(App_Search_HealthArchivesSrhForm));

    // 检索按钮 click 标志    
    private const string COMMAND_SEARCH = "DoSearch";

    // page load
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            initPageControls();

            // 取得当前登录的医生id信息
            SysUserManager manager = new SysUserManager(Context);
            SysUserBean sysUserBean = manager.getSysUserBean();
            MstSysUserBean mstSysUserBean =
                sysUserBean == null ? null : sysUserBean.getMstSysUserBean();
            MstEmployeeBean mstEmpBean =
                sysUserBean == null ? null : sysUserBean.getEmployeeBean();
            string loginId = mstSysUserBean == null ? "" : mstSysUserBean.SysUserId;
            string emplyId = mstSysUserBean == null ? "" : mstSysUserBean.EmployeeId;
            string emplyRole = mstEmpBean == null ? "" : mstEmpBean.Role;
            
            ViewState["DOCTOR_ID"] = emplyId;

            // 接收传递参数并初始化控件
            txtMemId.Text = Convert.ToString(Context.Items["InpMemberId"]);
            txtName.Text = Convert.ToString(Context.Items["InpMemberName"]);
            lstSex.SelectedValue = Convert.ToString(Context.Items["InpSex"]);
            txtDisease.Text = Convert.ToString(Context.Items["InpDisease"]);

            // 接收传递参数为检索条件
            ViewState["HidMemberId"] = Convert.ToString(Context.Items["HidMemberId"]);
            ViewState["HidMemberName"] = Convert.ToString(Context.Items["HidMemberName"]);
            ViewState["HidSex"] = Convert.ToString(Context.Items["HidSex"]);
            ViewState["HidDisease"] = Convert.ToString(Context.Items["HidDisease"]);
            ViewState["PAGE_COMMAND"] = Convert.ToString(Context.Items["HidCommand"]);

            // 接收传递参数为分页编号
            if (Context.Items.Contains("HidPageIndex"))
            {
                viewSrhRst.PageIndex = Convert.ToInt32(Context.Items["HidPageIndex"]);
            }
        }

    }

    // before render
    void Page_PreRender(object sender, EventArgs e)
    {
        // reload list
        if (COMMAND_SEARCH.Equals(ViewState["PAGE_COMMAND"].ToString()))
        {
            HealthArchivesDao dao = new HealthArchivesDao();
            IList lstRst = null;
            HealthArchivesBean beanHA = new HealthArchivesBean();
            beanHA.MemberNo = Convert.ToString(ViewState["HidMemberId"]);
            beanHA.MemberName = Convert.ToString(ViewState["HidMemberName"]);            
            beanHA.MemberSex = Convert.ToString(ViewState["HidSex"]);
            beanHA.Disease = Convert.ToString(ViewState["HidDisease"]);
            lstRst = dao.ListMemberArchives(beanHA);
            if (lstRst == null || lstRst.Count == 0)
            {
                ((Design_MasterPage)this.Master).InfoMessage =
                    HttpContext.GetGlobalResourceObject(Constants.GLOBAL_RESOURCE_NAME, "E0005").ToString();
            }
            viewSrhRst.DataSource = lstRst;
            viewSrhRst.DataBind();
        }        
    }

    // 检索按钮
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        ViewState["HidMemberId"] = txtMemId.Text;
        ViewState["HidMemberName"] = txtName.Text;
        ViewState["HidSex"] = "";
        if (lstSex.Items.Count > 0 && lstSex.SelectedIndex > 0)
        {
            ViewState["HidSex"] = lstSex.SelectedValue;
        }
        ViewState["HidDisease"] = txtDisease.Text;
        ViewState["PAGE_COMMAND"] = COMMAND_SEARCH;
        viewSrhRst.PageIndex = 0;
    }

    // page index change
    protected void viewSrhRst_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        // 设置分页编号
        viewSrhRst.PageIndex = e.NewPageIndex;
    }

    // details click
    protected void viewSrhRst_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (Request["x"] == null || Request["y"] == null)
            return;

        //string cureId = "";
        //string memberId = "";
        //string url = "~/App/Input/HealthTherapySrhForm.aspx";
        //if (e.CommandName == "Plan")
        //{
        //    int index = Convert.ToInt32(e.CommandArgument);
        //    // Get the Key value from the GridView control.
        //    DataKey key = viewSrhRst.DataKeys[index];
        //    memberId = Convert.ToString(key.Value);
        //    cureId = Convert.ToString(key.Values[1]);
        //    url = "~/App/Input/HealthTherapyUpdForm.aspx";
        //}
        //else if (e.CommandName == "Details")
        //{
        //    int index = Convert.ToInt32(e.CommandArgument);
        //    // Get the Key value from the GridView control.
        //    DataKey key = viewSrhRst.DataKeys[index];
        //    memberId = Convert.ToString(key.Value);
        //    cureId = Convert.ToString(key.Values[1]);
        //    url = "~/App/Input/HealthTherapyDetailSrhForm.aspx";
        //}
        //// 传递分页编号
        //Context.Items["HidPageIndex"] = viewSrhRst.PageIndex;
        //Context.Items["CURE_ID"] = cureId;
        //Context.Items["MEMB_ID"] = memberId;
        ////Response.Redirect("~/App/Input/HealthTherapyUpdForm.aspx?CURE_ID=" + cureId);
        //ControllerUtils.Transfer(Context, url, true);

    }

    // adjust bind data
    protected void viewSrhRst_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            //if (string.IsNullOrEmpty(((HealthTherapyBean)e.Row.DataItem).ApartmentNo))
            //    e.Row.Cells[COLUMN_ROOMNO].Text = "/";
            //if (string.IsNullOrEmpty(((HealthTherapyBean)e.Row.DataItem).CureType))
            //    e.Row.Cells[COLUMN_CURETYPE].Text = "/";
            //if (string.IsNullOrEmpty(((HealthTherapyBean)e.Row.DataItem).CureName))
            //    e.Row.Cells[COLUMN_CURENAME].Text = "/";
        }
    }

    // 操作 link click
    protected void LinkButton_Command(object sender, CommandEventArgs e)
    {
        string directUrl;
        switch (e.CommandName)
        {
            case "VISIT":   //家访
                directUrl = "~/App/Search/HealthArchivesVstSrhForm.aspx";
                break;
            case "CURE":    //治疗
                directUrl = "~/App/Search/HealthArchivesTrpSrhForm.aspx";
                break;
            case "MEDICINE"://用药
                directUrl = "~/App/Search/HealthArchivesMdcSrhForm.aspx";
                break;
            case "REPORT":  //体检报告
                directUrl = "~/App/Search/HealthArchivesPhySrhForm.aspx";
                break;
            case "BRIEF":   //健康状况
                ViewState["MEMBER_NO"] = Convert.ToString(e.CommandArgument);
                healthDetail();
                return;
            default:
                directUrl = "~/App/Search/HealthArchivesSrhForm.aspx";
                break;
        }

        // Transfer Page        
        Context.Items["MEMBER_NO"] = Convert.ToString(e.CommandArgument);
        gotoPage(directUrl);
    }

    void healthDetail()
    {
        string memberno = Convert.ToString(ViewState["MEMBER_NO"]);
        Response.Write("<script language=JavaScript>" + "window.open('../Input/HealthMemberHealthForm.aspx?MEMBER_NO=" + memberno + "','HealthMemberHealthForm'" + ",'scrollbars,resizable');</script>");
    }

    // 参数传递
    private void gotoPage(string url)
    {
        // 传递控件输入内容
        Context.Items["InpMemberId"] = txtMemId.Text;
        Context.Items["InpMemberName"] = txtName.Text;
        Context.Items["InpSex"] = lstSex.SelectedValue;
        Context.Items["InpDisease"] = txtDisease.Text;
             
        // 传递检索条件
        Context.Items["HidMemberId"] = Convert.ToString(ViewState["HidMemberId"]);
        Context.Items["HidMemberName"] = Convert.ToString(ViewState["HidMemberName"]);
        Context.Items["HidSex"] = Convert.ToString(ViewState["HidSex"]);
        Context.Items["HidDisease"] = Convert.ToString(ViewState["HidDisease"]);
        Context.Items["HidCommand"] = Convert.ToString(ViewState["PAGE_COMMAND"]);
       
        // 传递分页编号
        Context.Items["HidPageIndex"] = viewSrhRst.PageIndex;

        ControllerUtils.Transfer(Context, url, true);
    }

    // bind common catalog data 
    private void initPageControls()
    {
        HealthArchivesDao dao = new HealthArchivesDao();
        CommonDictionaryBean beanComDict = new CommonDictionaryBean();
        IList lstCat = null;

        // 性别
        log.Fatal("\n ***** get SEX catalog *****");
        beanComDict.Catalog = "SEX";
        lstCat = dao.FindByCatalog(beanComDict);
        lstSex.Items.Clear();  
        bindDataToListControl("lstSex", lstCat);
    }

    // bind db data to selected controls
    private void bindDataToListControl(string ControlId, IList LstData)
    {
        if (LstData == null || LstData.Count == 0) return;

        foreach (CommonDictionaryBean bean in LstData)
        {
            if (this.Master.FindControl("ContentPlaceHolder1").FindControl(ControlId) is DropDownList)
            {
                ((DropDownList)this.Master.FindControl("ContentPlaceHolder1").FindControl(ControlId)).Items.Add(new ListItem(bean.Content, bean.DictNo.ToString()));
            }
            else if ((this.Master.FindControl("ContentPlaceHolder1").FindControl(ControlId) is CheckBoxList)
                || (this.Master.FindControl("ContentPlaceHolder1").FindControl(ControlId) is RadioButtonList))
            {
                ((ListControl)this.Master.FindControl("ContentPlaceHolder1").FindControl(ControlId)).Items.Add(new ListItem(bean.Content, bean.DictNo.ToString()));
            }
        }
    }

   
}
